package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.DictionaryMapper;
import com.zhijian.medical.entity.po.Dictionary;
import com.zhijian.medical.entity.vo.request.DictionarySearchVo;
import com.zhijian.medical.entity.vo.response.DictionaryVo;
import com.zhijian.medical.service.DictionaryService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 字典表业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-06-05 19:55:21
 */
@Slf4j
@Service
public class DictionaryServiceImpl implements DictionaryService {

    @Autowired
    private DictionaryMapper mapper;

    @Override
    public Dictionary selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public DictionaryVo selectVoByType(String type) {
        return mapper.selectVoByType(type);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(Dictionary dictionary) {
        Date now = new Date();
        dictionary.setCreateTime(now);
        dictionary.setUpdateTime(now);
        return mapper.insert(dictionary) > 0 ? dictionary.getId() : 0L;
    }

    @Override
    public boolean update(Dictionary dictionary) {
        Date now = new Date();
        dictionary.setUpdateTime(now);
        return mapper.updateByPrimaryKey(dictionary) != 0;
    }

    @Override
    public Integer count(DictionarySearchVo dictionarySearchVo) {
        return mapper.count(dictionarySearchVo);
    }

    @Override
    public List<DictionaryVo> selectList(DictionarySearchVo dictionarySearchVo) {
        return mapper.selectList(dictionarySearchVo);
    }

    @Override
    public Page<DictionaryVo> selectPage(DictionarySearchVo dictionarySearchVo) {
        //先直接查总数
        int count = count(dictionarySearchVo);
        //分页
        Page<DictionaryVo> page = new Page<>(true, count, dictionarySearchVo.getPageNum(), dictionarySearchVo.getPageSize());
        dictionarySearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<DictionaryVo> responseVos = this.selectList(dictionarySearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean updateByPrimaryKeySelective(Dictionary record) {
        record.setUpdateTime(new Date());
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

}